package com.weipu.common.okhttp.request;

import java.util.Map;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.weipu.common.constants.Constant.HttpMethod;
import com.weipu.common.okhttp.JsonUtils;
import com.weipu.common.util.Logger;

/**
 * JSON类型的 Request 构造类
 * @author huangfei
 *
 */
public class JsonOkHttpRequest extends OkHttpRequest {
	// 网络请求媒体类型
	private MediaType mediaType = MediaType
			.parse("application/json; charset=utf-8");

	public JsonOkHttpRequest(String url, Object tag, Map<String, String> headers) {
		super(url, tag, headers);
	}

	/**
	 * 获取Request对象
	 * 
	 * @param method
	 *            请求方法
	 * @param params
	 *            请求参数 Bean对象
	 * @return
	 */
	public Request getRequestIntance(HttpMethod method, Object params) {
		RequestBody requestBody = buildRequestBody(params);
		return buildRequest(requestBody, method);
	}

	/**
	 * 通过请求参数构建RequestBody
	 * @param params 请求参数Bean对象
	 * @return
	 */
	protected RequestBody buildRequestBody(Object params) {
		return RequestBody.create(mediaType,
				JsonUtils.getJsonStringByBean(params));
	}

}
